/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef WebSocketServer_H
#define WebSocketServer_H

#include <QtCore/QObject>
#include <QtCore/QList>
#include <QtCore/QByteArray>
#include "websocketserver_global.h"
#include "MacroDefine/GeneralDefine.h"

QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
QT_FORWARD_DECLARE_CLASS(QWebSocket)

//#ifndef CALLBACKTYPE
//#define CALLBACKTYPE
//typedef void (*CallBackRecvText)(QWebSocket *pClient,const QString& outRecvData);
//typedef void (*CallBackRecvBinary)(QWebSocket *pClient,const QByteArray& outRecvData);
//#endif

class PowerManager;
/**
 * @brief The WebSocketServer class webserver服务器类
 */
class WEBSOCKETSERVERSHARED_EXPORT WebSocketServer
{
public:
    WebSocketServer(PowerManager* powerManageIn);
    ~WebSocketServer();

public:


private:
    //设置类命令
    int process_manual_move(QJsonObject &redeiveJsonObjIn);
    int process_manual_stop(QJsonObject &redeiveJsonObjIn);
    int process_set_servo(QJsonObject &redeiveJsonObjIn);
    int process_set_clear_msg(QJsonObject &redeiveJsonObjIn);
    int process_set_mode(QJsonObject &redeiveJsonObjIn);
    int process_set_manual_ratio(QJsonObject &redeiveJsonObjIn);
    int process_set_auto_ratio(QJsonObject &redeiveJsonObjIn);
    int process_set_run(QJsonObject &redeiveJsonObjIn);
    int process_set_cease(QJsonObject &redeiveJsonObjIn);
    int process_set_stop(QJsonObject &redeiveJsonObjIn);
    int process_set_clear_odomter(QJsonObject &redeiveJsonObjIn);
    int process_set_create_map_start(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_set_save_map(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_set_load_map(QJsonObject &redeiveJsonObjIn);
    int process_set_vehicle_initial_position(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_set_vmark_type(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);

    //获取类命令
    int process_get_battery_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_login_in(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_basic_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_msg_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_map_list(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);

    int process_get_di_name_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_dido_value_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_do_name_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);

    int process_set_do(QJsonObject &redeiveJsonObjIn);
    int process_set_current_running_program_file(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_robot_program_file_list(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_robot_running_program_files_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_robot_program_files_strings(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_set_record_dpoint(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    int process_get_dpoint(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //超声波检测获取
    int process_get_ultrasonic_sensor_check_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //点云检测获取
    int process_get_point_cloud_check_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //设置超声波检测
    int process_set_ultrasonic_sensor_check_enable(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //设置点云检测
    int process_set_point_cloud_check_enable(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //获取版本信息
    int process_get_version_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //获取注册信息
    int process_get_register_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //设置注册
    int process_set_register(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //移动到点,需要修改webhmi的语法提取，因为语法改了
    int process_move_to_navgation_point(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //设置VMARK-LASER FILTER TYPE
    int process_set_vmark_laser_filter_type(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //获取VMARK
    int process_get_vmark_debug_info(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //设置当前程序运行行号
    int process_set_program_line_number(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //设置当前默认程序
    int process_set_default_program_name(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //设置当前默认地图
    int process_set_default_map_name(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //关机指令
    int process_set_shutdown(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
    //单台车辆呼叫命令
    int process_set_call_vehicle(QJsonObject &redeiveJsonObjIn,QWebSocket *pClientIn);
friend class EchoServer;
private:

    QString  longTimeToTimeString(long timeIn);
    int initial();

    /**
     * @brief CloseClientSocket 关闭客户端连接
     * @param pClient
     */
    int closeClientSocket(QWebSocket *pClient);
    /**
     * @brief CreateWebSocketServer 创建服务器
     * @param localPort
     */
    int createWebSocketServer(int localPort);
    /**
     * @brief destroyWebSocketServer 销毁服务器
     */
    int destroyWebSocketServer();
//    /**
//     * @brief SetCallBackRecvText 网页请求回调处理
//     * @param inCallBackRecvText
//     */
//    void setCallBackRecvText(CallBackRecvText inCallBackRecvText);
//    void setCallBackRecvBinary(CallBackRecvBinary inCallBackRecvText);

    /******************************************
     * return value
     *  -1 send faild
     *  >0 Length of data sent successfully
    *******************************************/
    qint64 sendMsgText(QWebSocket *pClient,const QString& intSendData);
    /******************************************
     * return value
     *  -1 send faild
     *  >0 Length of data sent successfully
    *******************************************/
    qint64 sendMsgBinary(QWebSocket *pClient,const QByteArray& intSendData);



    void funcCallBackRecvBinary(QWebSocket *pClient,const QByteArray& outRecvData);

    void funcCallBackRecvText(QWebSocket *pClient,const QString& outRecvData);



private:
    QObject* objWebSocketServer;
    PowerManager* powerManage;
};

#endif // WebSocketServer_H
